<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: XListMenu.php 289 2008-11-14 05:34:40Z steen.rabol@gmail.com $
 *
 */

Prado::using('System.Web.UI.WebControls.TWebControl');

class XListMenu extends TWebControl
{
	private $_activeCssClass	= "";
	private $_inactiveCssClass	= "";

	public function getActiveCssClass()
	{
		return $this->_activeCssClass;
	}

	public function setActiveCssClass($value)
	{
		$this->_activeCssClass = $value;
	}

	public function getInActiveCssClass()
	{
		return $this->_inactiveCssClass;
	}

	public function setInActiveCssClass($value)
	{
		$this->_inactiveCssClass = $value;
	}

	public function addParsedObject($object)
	{
		if($object instanceof XListMenuItem)
		{
			$this->Controls[] = $object;
		}
		else
			throw new TInvalidDataTypeException(get_class($object) . " is not supported by " . get_class($this));
	}

	public function render($writer)
	{
		$activeClass		= $this->ActiveCssClass;
		$inactiveClass		= $this->InActiveCssClass;
		$currentPagePath	= $this->getPage()->getPagePath();
		$cssClass			= $this->CssClass;

		if(($activeClass=$this->ActiveCssClass) !== '')
			$activeClass=' class="'.$activeClass.'"';

		if(($inactiveClass=$this->InactiveCssClass)!=='')
			$inactiveClass=' class="'.$inactiveClass.'"';

		if(($cssClass = $this->CssClass) !== '')
			$cssClass = ' class="' . $this->CssClass . '"';

		$writer->write("<ul " . trim($cssClass) . ">");

		$controls = $this->Controls;

		foreach($controls as $item)
		{
			if($item->getEnabled())
			{
				$pagePath=$item->getPagePath();
				if($pagePath[strlen($pagePath)-1]==='*')
				{
					if(strpos($currentPagePath.'.',rtrim($pagePath,'*'))===0)
						$cssClass=$activeClass;
					else
						$cssClass=$inactiveClass;
				}
				else
				{
					if($pagePath===$currentPagePath)
						$cssClass=$activeClass;
					else
						$cssClass=$inactiveClass;
				}

				if(($url=$item->getNavigateUrl())==='')
					$url=$this->getService()->constructUrl($pagePath);

				$writer->write("<li><a href=\"$url\"$cssClass title=\"" . $item->getText() . "\">".$item->getText()."</a></li>\n");
			}
		}
		$writer->write("</ul>");
	}
}

class XListMenuItem extends TWebControl
{
	private	$_pagePath;
	private $_text;

	public function getText()
	{
		return $this->_text;
	}

	public function setText($value)
	{
		$this->_text = $value;
	}

	public function getPagePath()
	{
		return $this->_pagePath;
	}

	public function setPagePath($value)
	{
		$this->_pagePath = $value;
	}

	public function getNavigateUrl()
	{
		return $this->hasAttribute('NavigateUrl')?$this->getAttribute('NavigateUrl'):'';
	}

	public function setNavigateUrl($value)
	{
		$this->setAttribute('NavigateUrl',$value);
	}
}
?>